package cn.wolfcode.wechat.controller;

import cn.wolfcode.wechat.service.ICouponService;
import cn.wolfcode.wechat.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by A on 2017/12/29.
 */
@Controller
public class CouponController {

    @Autowired
    private ICouponService couponService;

    private String openId;
    private Long cilentId;

    @RequestMapping("/lottery")
    public String lotteryPage() {

        return "lottery/lottery";
    }

    @RequestMapping("/questionnaire")
    public String questionnaire(){

        return "questionnaire/questionnaire";
    }

    @RequestMapping("/coupon")
    public String couponPage(Model model){
        openId = WechatControllor.OPENID;



        return "coupon/coupon";
    }


    @RequestMapping("/saveCoupon")
    @ResponseBody
    public JSONResult saveCoupon(Long couponId){
        openId = WechatControllor.OPENID;
        try {
            cilentId = couponService.selectCilentIdByOpenId(openId);
            couponId = couponService.selectCouponIdByPrizeNum(couponId);

            int count = couponService.selectRelation(cilentId,couponId);
            if (count > 0) {
                return new JSONResult();
            }else {
                couponService.saveCoupon(cilentId,couponId);
                return new JSONResult();
            }
        }catch (Exception e){
            e.printStackTrace();
            return new JSONResult(false,e.getMessage());
        }
    }
}
